﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utils
{
    public static class Extensions
    {
        public static string ToHex(this string value, Encoding enc)
        {
            // Encode the array of chars.
            StringBuilder returnString = new StringBuilder(string.Empty);
            foreach (var byteValue in enc.GetBytes(value))
                returnString.Append(String.Format("{0:X2}", byteValue));
            return returnString.ToString();
        }

        public static string ToVietnameseWithoutAccent(this string text)
        {
            if (!string.IsNullOrEmpty(text))
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\p{IsCombiningDiacriticalMarks}+");
                string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);
                return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
            }
            return text;
        }
    }
}
